#include "VStdAfx.h"
#include "KLight.h"
#include "KLightRendering.h"
#include "KScene.h"

VLight::~VLight()
{

}

void VLight::UpdateWorldMatrix() const
{
	const VTransform& WorldTransform = GetWorldTransform();
	WorldTransform.ToMatrix(m_WorldMatrix);
	m_WorldMatrixDirty = 0;
}

void VLight::OnTransformDirty()
{
	m_WorldMatrixDirty = 1;
}

BOOL VLight::Load(const VXMLElement* Element)
{
	if (!VSceneObject::Load(Element))
	{
		return FALSE;
	}

	const VXMLElement* xLight = Element->FirstChildElement("Light");
	if (!xLight)
	{
		return FALSE;
	}

	m_CasterShadow = VXML_LoadBool(xLight, "CastShadow", FALSE);
	m_Color = VXML_Load(xLight, "Color");

	UpdateWorldMatrix();

	return TRUE;
}

void VLight::Save(VXMLElement* Element)
{

}

VMemPool<KLightInteraction> KLightInteraction::sm_MemPool;



void KLightAgent::OnShadowCasterRemoved(VSceneEntityAgent* ShadowCaster)
{

}

void KLightAgent::RT_OnAddToScene()
{
	VASSERT(m_Scene);
	m_Scene->GetSceneSpatial()->AddLight(this);


	// 
	VStackMemMark MemMark(VRenderStackPool);
	VRSPPODArray<KEntityAgentSceneInfo*> AffectNodes;
	VBoundBox BoundBox;//(m_BoundSphere);
	m_Scene->GetSceneSpatial()->GetIntersectNodes(BoundBox, AffectNodes);


	KLightInteraction* LightInteraction;
	for (UINT Index = 0; Index < AffectNodes.Size(); ++Index)
	{
		LightInteraction = new KLightInteraction;
//		LightInteraction->Attach(this, AffectNodes[Index]);
	}

}

void KLightAgent::RT_OnRemoveFromScene()
{
	VASSERT(m_Scene);
	m_Scene->GetSceneSpatial()->RemoveLight(this);

	// clear shadow casters .



}



//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
